package com.foreveross.fetl.core.domain.task;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Query;

import org.springframework.transaction.annotation.Transactional;

import com.dayatang.domain.InstanceFactory;
import com.foreveross.fetl.common.GlobalsConst;
import com.foreveross.fetl.common.domain.BaseEntity;
import com.foreveross.fetl.core.domain.task.repository.ITaskTemplateRepository;

/**
 * 抓取任务执行队列模板
 * <p>
 * 根据该模板生成<@link>TaskQueueEntity</link>
 * 
 * @author xiangsf 2013-2-17
 * 
 */
@javax.persistence.Table(name = "CRAWL_TASK_TEMPLATE")
@javax.persistence.Entity
public class TaskTemplateEntity  extends BaseEntity{
	/**
	 * 
	 */
	private static final long serialVersionUID = -4448819408322145384L;
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "ID")
	private Long id;
	/**
	 * 任务名称
	 */
	@Column(name = "NAME")
	private String name;

	/**
	 * 起点城市区域码
	 */
	@Column(name = "AREA_CODE")
	private String areaCode;
	/**
	 * 起点城市区域名称
	 */
	@Column(name = "AREA_NAME")
	private String areaName;
	/**
	 * 起点城市
	 */
	@Column(name = "FROM_CITY")
	private String fromCity;

	@Column(name = "FROM_CITY_NAME")
	private String fromCityName;

	@Column(name = "TO_CITY")
	private String toCity;

	@Column(name = "TO_CITY_NAME")
	private String toCityName;

	@Column(name = "GRAB_CHANNEL_ID")
	private Long grabChannelId;

	@Column(name = "GRAB_CHANNEL")
	private String grabChannel;

	/**
	 * 附加的hbase应用值，用于生成rowkey,以便于应用根据该值统计
	 * <p>实际上使用下面的"抓取渠道ID"也可，但基于应用已完成部分功能，补充上该值
	 */
	@Column(name = "ATTACH_HBASE_KEY")
	private String attachHbaseKey;
	
	@Column(name = "FREQUENCY")
	private int frequency;

	@Column(name = "NEXT_GRAB_TIME")
	private Date nextGrabTime;

	@Column(name = "VALIDITY_TO")
	private Date validityTo;

	@Column(name = "VALIDITY_FROM")
	private Date validityFrom=new Date();

	@Column(name = "STATUS")
	private Integer status = GlobalsConst.DATA_STATUS_NORMAL;

	@Column(name = "PRIORITY")
	private int priority;

	@Column(name = "GRAB_SCOPE_END")
	private int grabScopeEnd;

	@Column(name = "GRAB_SCOPE_BEGIN")
	private int grabScopeBegin;
	// 是否抓回程
	@Column(name = "IS_RETURN")
	private Integer isReturn = GlobalsConst.DATA_LOGIC_Y;

	@Column(name = "RETURN_DATE_SCOPE")
	private int returnDateScope;

	@Column(name = "USER_ID")
	private Long userId;

	@Column(name = "USER_NAME")
	private String userName;
	
	@Column(name = "REMARK")
	private String remark;

	@Column(name = "CREATE_TIME")
	private Date createTime = new Date();
	
	/**
	 * 是否属于国际航线
	 */
	@Column(name="IS_INTERNATIONAL")
	private Integer isInternational;

	public Integer getIsInternational() {
		return isInternational;
	}

	public void setIsInternational(Integer isInternational) {
		this.isInternational = isInternational;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getIsReturn() {
		return isReturn;
	}

	public void setIsReturn(Integer isReturn) {
		this.isReturn = isReturn;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getAreaCode() {
		return areaCode;
	}

	public void setAreaCode(String areaCode) {
		this.areaCode = areaCode;
	}

	public String getAreaName() {
		return areaName;
	}

	public void setAreaName(String areaName) {
		this.areaName = areaName;
	}

	public Long getGrabChannelId() {
		return grabChannelId;
	}

	public void setGrabChannelId(Long grabChannelId) {
		this.grabChannelId = grabChannelId;
	}

	public String getFromCity() {
		return fromCity;
	}

	public void setFromCity(String fromCity) {
		this.fromCity = fromCity;
	}

	public String getFromCityName() {
		return fromCityName;
	}

	public void setFromCityName(String fromCityName) {
		this.fromCityName = fromCityName;
	}

	public String getToCity() {
		return toCity;
	}

	public void setToCity(String toCity) {
		this.toCity = toCity;
	}

	public String getToCityName() {
		return toCityName;
	}

	public void setToCityName(String toCityName) {
		this.toCityName = toCityName;
	}

	public String getGrabChannel() {
		return grabChannel;
	}

	public void setGrabChannel(String grabChannel) {
		this.grabChannel = grabChannel;
	}

	public int getFrequency() {
		return frequency;
	}

	public void setFrequency(int frequency) {
		this.frequency = frequency;
	}

	public Date getNextGrabTime() {
		return nextGrabTime;
	}

	public String getAttachHbaseKey() {
		return attachHbaseKey;
	}

	public void setAttachHbaseKey(String attachHbaseKey) {
		this.attachHbaseKey = attachHbaseKey;
	}

	public void setNextGrabTime(Date nextGrabTime) {
		this.nextGrabTime = nextGrabTime;
	}

	public Date getValidityTo() {
		return validityTo;
	}

	public void setValidityTo(Date validityTo) {
		this.validityTo = validityTo;
	}

	public Date getValidityFrom() {
		return validityFrom;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public void setValidityFrom(Date validityFrom) {
		this.validityFrom = validityFrom;
	}

	public int getPriority() {
		return priority;
	}

	public void setPriority(int priority) {
		this.priority = priority;
	}

	public int getGrabScopeEnd() {
		return grabScopeEnd;
	}

	public void setGrabScopeEnd(int grabScopeEnd) {
		this.grabScopeEnd = grabScopeEnd;
	}

	public int getGrabScopeBegin() {
		return grabScopeBegin;
	}

	public void setGrabScopeBegin(int grabScopeBegin) {
		this.grabScopeBegin = grabScopeBegin;
	}


	public int getReturnDateScope() {
		return returnDateScope;
	}

	public void setReturnDateScope(int returnDateScope) {
		this.returnDateScope = returnDateScope;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public ITaskTemplateRepository getTaskTemplateRepository() {
		return InstanceFactory.getInstance(ITaskTemplateRepository.class);
	}
	
public List<TaskTemplateEntity>  findGralChannelAirLine(String grabChannel){
	return getTaskTemplateRepository().findGralChannelAirLine(grabChannel);
}
	
	@Transactional
	public void saveOrUpdate(TaskTemplateEntity entity) {
		if (null == entity.getAttachHbaseKey()) {
			throw new IllegalArgumentException("渠道不能为空");
		}
		if (null == entity.getFromCity()) {
			throw new IllegalArgumentException("出发城市不能为空");
		}
		if (null == entity.getToCity()) {
			throw new IllegalArgumentException("到达城市不能为空");
		}
		if(null != entity.getId()){getTaskTemplateRepository().update(entity);return;}
		getTaskTemplateRepository().save(entity);
	}

	@Transactional
	public void delete(TaskTemplateEntity entity){
		getTaskTemplateRepository().delete(entity);
	}

	public List<TaskTemplateEntity> findByCondition(String channel,String fromCity, String toCity) {
		return getTaskTemplateRepository().findByCondition(channel,fromCity, toCity);
	}

	public Query createQuery(String hql,Object[] params){
		return getTaskTemplateRepository().createQuery(hql, params);
	}
	
	public TaskTemplateEntity get(Integer id){
		return getTaskTemplateRepository().find(TaskTemplateEntity.class, id);
	}
}
